const router = require('koa-router')();
var sc= require("../../mongodb/schema/easy/commonweal.js");
var scc= require("../../mongodb/schema/easy/cwsave.js");
var cwxm= require("../../mongodb/schema/easy/cwxm.js");

router.get('/commonweal', async (ctx, next) => {
    
let order=ctx.query.order,
    data=ctx.query.data;

   switch(order){
   	
   	   case 'reg':

       data[2]=data[2].split(',')

      
   	   let user = new sc({
			        user : data[0],                 //用户账号
			        password: data[1],                            //密码
			        other:data[2],
			        logindate : new Date()                      //最近登录时间
			    });
			    
	  const user_reg=await user.save()

    
	  if(user_reg){ctx.body=true}

   	   break;

   	   case 'login':
				   	var query3 = {
						user:data[0],
						password:data[1],
					};


			   const user_login  =await	sc.find(query3);

   	            
   	          if(user_login!=""){ctx.body=user_login[0].user}
   	   
   	   break;
   	   
   	
   	   case 'save':
   	        data[1]=data[1].split(',')
   	    var user1 = new scc({
			        user : data[0],                 //用户账号
			        other: data[1],                            //密码
			        logindate : new Date()                      //最近登录时间
			    });
   	   
   	   const  user1_save=await user1.save()
   	          
   	          ctx.body=true
   	   
   	   break;
   	   
   	   
   	   case 'saveIp':
   	   
   	      var user2 = new scc({
			        user : data[0],                 
			        other: data,                         
			        logindate : new Date()                    
			    });
   	   
   	    const user2_ip =await user2.save()
   	    
   	     if(user2_ip){ctx.body=true}
   	   
   	   break;
   	   
   	   
   	   
   	   case 'getMaster':
   	   
   	   

			 let query2 = {
						user: {$ne:'小美'}
					};		
		const uuo=await scc.find(query2);			
   	  ctx.body=uuo
   	   
   	   break;
   	   
   	   
   	   case 'getU':
   	   
   	 let query = {
						user: {$ne:'admin'}
					};
					
					
					
	   ctx.body=await scc.find(query);					
					
   	   break;
   	   
   	   
   	   
   	    case 'username':
   	   
   	   var query4 = {
						user: {$ne:'admin'}
					};

		ctx.body=await sc.find(query4);					
					
   	   break;
   	   
   	   
   	   
   	   
   	   case 'cwxm':
   	       data[1]=data[1].split(',')
		   	var conditions = {address: data[0]};  
		    var updates = {$set: {data:data[1]}};

			const user_update =await cwxm.update(conditions,updates)
			 
			if(user_update){ctx.body=true}
   	   
   	   
   	   break;
   	   
   	   
   	   
   	   case 'getxm':
			
	   ctx.body=await cwxm.find({});				
   	   
   	   break;

   	   case 'getxmC':
   	   
   	   var query5 = {
						address: data[0]
					};
					
					
					
		ctx.body=await cwxm.find(query5);					
					
   	   break;
   	   
   	   
   	   
   	   
   	   
   	   
   	   
   	   
   	   
   	   
   	   
   	   
   	   
   	   
   	   
   	   
   	   
   	   
   	
   	
   	
   }











  
  
});




module.exports = router;
